package com.conference.controller;

import com.conference.entity.ConferenceVenue;
import com.conference.service.ConferenceVenueService;
import com.conference.entity.UserConference;
import com.conference.service.UserConferenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import com.conference.dto.SeatUpdateDTO;

@RestController
@RequestMapping("/api/conferences/{conferenceId}/venues")
public class ConferenceVenueController {

    @Autowired
    private ConferenceVenueService venueService;

    @Autowired
    private UserConferenceService userConferenceService;

    @PostMapping
    public ResponseEntity<ConferenceVenue> createVenue(
        @PathVariable Long conferenceId,
        @RequestBody ConferenceVenue venue
    ) {
        venue.setConferenceId(conferenceId);
        return ResponseEntity.ok(venueService.createVenue(venue));
    }

    @PutMapping("/{id}")
    public ResponseEntity<ConferenceVenue> updateVenue(
        @PathVariable Long conferenceId,
        @PathVariable Long id,
        @RequestBody ConferenceVenue venue
    ) {
        venue.setId(id);
        venue.setConferenceId(conferenceId);
        return ResponseEntity.ok(venueService.updateVenue(venue));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteVenue(@PathVariable Long id) {
        venueService.deleteVenue(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{id}")
    public ResponseEntity<ConferenceVenue> getVenue(@PathVariable Long id) {
        return ResponseEntity.ok(venueService.getVenueById(id));
    }

    @GetMapping
    public ResponseEntity<List<ConferenceVenue>> getVenues(@PathVariable Long conferenceId) {
        return ResponseEntity.ok(venueService.getVenuesByConferenceId(conferenceId));
    }

    @GetMapping("/search")
    public ResponseEntity<Page<ConferenceVenue>> searchVenues(
        @PathVariable Long conferenceId,
        @RequestParam(required = false) String venueName,
        @RequestParam(defaultValue = "0") int page,
        @RequestParam(defaultValue = "10") int size
    ) {
        Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createTime"));
        return ResponseEntity.ok(venueService.searchVenues(conferenceId, venueName, pageable));
    }

    @GetMapping("/{venueId}/import-seats")
    public ResponseEntity<?> importSeats(
        @PathVariable Long conferenceId, 
        @PathVariable Long venueId
    ) {
        try {
            // 获取会议的参会人员信息
            List<UserConference> participants = userConferenceService.getConferenceParticipants(conferenceId);
            
            // 更新场地布局
            userConferenceService.updateVenueLayout(conferenceId, venueId, participants);
            
            // 返回参会人员信息用于前端显示
            return ResponseEntity.ok(participants);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("导入座位数据失败: " + e.getMessage());
        }
    }

    @PutMapping("/{venueId}/seats")
    public ResponseEntity<?> updateSeat(
        @PathVariable Long conferenceId,
        @PathVariable Long venueId,
        @RequestBody SeatUpdateDTO seatUpdateDTO
    ) {
        try {
            seatUpdateDTO.setMeetingId(conferenceId);
            UserConference updatedParticipant = userConferenceService.updateParticipantSeat(seatUpdateDTO);
            
            // 更新场地布局
            List<UserConference> allParticipants = userConferenceService.getConferenceParticipants(conferenceId);
            userConferenceService.updateVenueLayout(conferenceId, venueId, allParticipants);
            
            return ResponseEntity.ok(updatedParticipant);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("更新座位信息失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{venueId}/seats")
    public ResponseEntity<?> deleteSeat(
        @PathVariable Long conferenceId,
        @PathVariable Long venueId,
        @RequestParam Integer rowNum,
        @RequestParam Integer seatNum
    ) {
        try {
            userConferenceService.deleteParticipantSeat(conferenceId, rowNum, seatNum);
            
            // 更新场地布局
            List<UserConference> allParticipants = userConferenceService.getConferenceParticipants(conferenceId);
            userConferenceService.updateVenueLayout(conferenceId, venueId, allParticipants);
            
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("删除座位信息失败: " + e.getMessage());
        }
    }
} 